home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8062 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  54 lines

  1. Path: news.eclipse.net!usenet
  2. From: steve@eclipse.net (Steve Teale)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: A simple question for all the C++ gurus out there!
  5. Date: Wed, 14 Feb 1996 02:23:10 GMT
  6. Message-ID: <4frh58$pbk@lunar.eclipse.net>
  7. References: <3120F95F.659@iglou.com>
  8. NNTP-Posting-Host: ne_37.eclipse.net
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. "Abe L. Getchell" <panther@iglou.com> wrote:
  12.  
  13. >I am trying to write a public member function that will use a private
  14. >data member from which it inherited from the base class.  This won't compile.
  15. >It gives me an error message like "A::a private data member not accessible in
  16. >class B".  Why won't this work if the prvate data member being inherited from
  17. >the base class A be a private data member of the derived class?
  18.  
  19. >Abe L. Getchell
  20.  
  21. >Please respond via E-Mail if possible...
  22.  
  23. That's just part of what private means.
  24.  
  25. If you want members of the base class to be generally private, but
  26. available to derived classes, use protected.
  27.  
  28. class A {
  29.     ...
  30. private:
  31.     int a;
  32. };
  33.  
  34. class B : public A {
  35. public:
  36.     void foo();
  37. private:
  38.     ....
  39. };
  40.  
  41. void B::foo()
  42. {
  43.     a++;    // error - a is not accessible
  44. }
  45.  
  46. Use instead
  47.  
  48. class A {
  49.     ...
  50. protected:
  51.     int a;
  52. };
  53.  
  54.